package com.ruoyi.web.controller.recipe;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.recipe.domain.RecipeCategory;
import com.ruoyi.recipe.domain.RecipeTagCategory;
import com.ruoyi.recipe.service.IRecipeCategoryService;
import com.ruoyi.system.domain.SysUserRole;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.recipe.domain.RecipeTag;
import com.ruoyi.recipe.service.IRecipeTagService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 食谱类别标签Controller
 * 
 * @author ruoyi
 * @date 2024-11-10
 */
@Controller
@RequestMapping("/recipe/tag")
public class RecipeTagController extends BaseController
{
    private String prefix = "recipe/tag";

    @Autowired
    private IRecipeTagService recipeTagService;

    @Autowired
    private IRecipeCategoryService recipeCategoryService;

    @RequiresPermissions("recipe:tag:view")
    @GetMapping()
    public String tag()
    {
        return prefix + "/tag";
    }

    /**
     * 查询食谱类别标签列表
     */
    @RequiresPermissions("recipe:tag:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecipeTag recipeTag)
    {
        startPage();
        List<RecipeTag> list = recipeTagService.selectRecipeTagList(recipeTag);
        return getDataTable(list);
    }

    /**
     * 导出食谱类别标签列表
     */
    @RequiresPermissions("recipe:tag:export")
    @Log(title = "食谱类别标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecipeTag recipeTag)
    {
        List<RecipeTag> list = recipeTagService.selectRecipeTagList(recipeTag);
        ExcelUtil<RecipeTag> util = new ExcelUtil<RecipeTag>(RecipeTag.class);
        return util.exportExcel(list, "食谱类别标签数据");
    }

    /**
     * 新增食谱类别标签
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存食谱类别标签
     */
    @RequiresPermissions("recipe:tag:add")
    @Log(title = "食谱类别标签", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecipeTag recipeTag)
    {
        return toAjax(recipeTagService.insertRecipeTag(recipeTag));
    }

    /**
     * 修改食谱类别标签
     */
    @RequiresPermissions("recipe:tag:edit")
    @GetMapping("/edit/{tagId}")
    public String edit(@PathVariable("tagId") Long tagId, ModelMap mmap)
    {
        RecipeTag recipeTag = recipeTagService.selectRecipeTagById(tagId);
        mmap.put("recipeTag", recipeTag);
        return prefix + "/edit";
    }

    /**
     * 修改保存食谱类别标签
     */
    @RequiresPermissions("recipe:tag:edit")
    @Log(title = "食谱类别标签", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecipeTag recipeTag)
    {
        return toAjax(recipeTagService.updateRecipeTag(recipeTag));
    }

    /**
     * 删除食谱类别标签
     */
    @RequiresPermissions("recipe:tag:remove")
    @Log(title = "食谱类别标签", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recipeTagService.deleteRecipeTagByTagIds(ids));
    }

    /**
     * 校验食谱类别标签名称
     */
    @PostMapping("/checkTagNameUnique")
    @ResponseBody
    public boolean checkTagNameUnique(RecipeTag recipeTag)
    {
        return recipeTagService.checkTagNameUnique(recipeTag);
    }

    /**
     * 分配类别
     */
    @RequiresPermissions("recipe:tag:edit")
    @GetMapping("/allocateCategory/{tagId}")
    public String allocateCategory(@PathVariable("tagId") Long tagId, ModelMap mmap)
    {
        mmap.put("tag", recipeTagService.selectRecipeTagById(tagId));
        return prefix + "/allocateCategory";
    }

    /**
     * 查询已分配类别列表
     */
    @RequiresPermissions("recipe:tag:list")
    @PostMapping("/allocateCategory/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(RecipeCategory category)
    {
        startPage();
        List<RecipeCategory> list = recipeCategoryService.selectAllocatedList(category);
        return getDataTable(list);
    }

    /**
     * 选择类别
     */
    @GetMapping("/allocateCategory/selectCategory/{tagId}")
    public String selectCategory(@PathVariable("tagId") Long tagId, ModelMap mmap)
    {
        mmap.put("tag", recipeTagService.selectRecipeTagById(tagId));
        return prefix + "/selectCategory";
    }

    /**
     * 查询未分配类别列表
     */
    @RequiresPermissions("recipe:tag:list")
    @PostMapping("/allocateCategory/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(RecipeCategory category)
    {
        startPage();
        List<RecipeCategory> list = recipeCategoryService.selectUnallocatedList(category);
        return getDataTable(list);
    }

    /**
     * 批量分类
     */
    @RequiresPermissions("recipe:tag:edit")
    @Log(title = "标签管理", businessType = BusinessType.GRANT)
    @PostMapping("/allocateCategory/selectAll")
    @ResponseBody
    public AjaxResult selectAllocateCategoryAll(Long tagId, String categoryIds)
    {
        return toAjax(recipeTagService.insertAllocateCategory(tagId, categoryIds));
    }

    /**
     * 取消分类
     */
    @RequiresPermissions("recipe:tag:edit")
    @Log(title = "标签管理", businessType = BusinessType.GRANT)
    @PostMapping("/allocateCategory/cancel")
    @ResponseBody
    public AjaxResult cancelAllocateCategory(RecipeTagCategory tagCategory)
    {
        return toAjax(recipeTagService.cancelAllocateCategory(tagCategory));
    }

    /**
     * 批量取消授分类
     */
    @RequiresPermissions("recipe:tag:edit")
    @Log(title = "标签管理", businessType = BusinessType.GRANT)
    @PostMapping("/allocateCategory/cancelAll")
    @ResponseBody
    public AjaxResult cancelAllocateCategories(Long tagId, String categoryIds)
    {
        return toAjax(recipeTagService.cancelAllocateCategories(tagId, categoryIds));
    }
}
